package com.itheima.user.controller;

import com.itheima.user.pojo.User;
import com.itheima.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Random;

@RestController
@RequestMapping("user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;

    @Value("${server.port}")
    int port;

    @GetMapping("/{id}")
    public User queryById(@PathVariable("id") String username, HttpServletRequest request) {
        int i = new Random().nextInt(5000);
        System.out.println("请求头的值:"+request.getHeader("name"));
        log.info("用户微服务被调用 休眠时间==> {}",i);
//        try {
//            Thread.sleep(5000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        User user = userService.queryById(username);
        user.setEmail("提供服务的端口: " + port);
        return user;
    }
    @PostMapping
    public String save(@RequestBody User user) {
        log.info("保存用户信息 ==> {}",user);
        return "保存用户信息成功";
    }
    @DeleteMapping("{id}")
    public String save(@PathVariable("id") String username) {
        log.info("删除用户信息 ==> {}",username);
        return "删除用户信息成功";
    }
}